#include "stdafx.h"
#include "MiniCppUnit.hxx"
#include "leudgrid/streamfix.h"
#pragma comment (lib, "ws2_32.lib")
#include "leudgrid/StreamBaseEx.h"
#include <vector>

struct ee
{
	int i ;
	short s;
	unsigned int ui;
	unsigned short us;
};
class TestStreamFix : public TestFixture<TestStreamFix>
{
public:
	TEST_FIXTURE( TestStreamFix )
	{
		TEST_CASE( testAssert );
		TEST_CASE( testAssertMessage );
		TEST_CASE( testAdditionInts );
		TEST_CASE( testStreamFix );
		TEST_CASE( testStreamFixRead );
		TEST_CASE( testStreamFlag );
		TEST_CASE( testStreamString );
	}
	void testAssert()
	{
		ASSERT(3==1+2);
	}
	void testAssertMessage()
	{
		ASSERT_MESSAGE( 2==1+1, "2 should be 1 plus 1 ");
	}
	void testAdditionInts()
	{
		ASSERT_EQUALS( 2, 1+1 );
	}
	void testStreamFix()
	{
		char buffer[200];
		LEUD::StreamFix stream(buffer,200);
		int i = 2;
		short s = 3;
		stream<<i<<s;
		ASSERT(stream);
		int io=0;
		short so=0;
		stream>>io>>so;
		ASSERT(stream);
		ASSERT(i==io);
		ASSERT(s==so);
	}
	void testStreamFixRead()
	{
		char buf[200];
		int i = 2;
		short s = 3;
		memcpy(buf,&i,sizeof(int));
		memcpy(buf+sizeof(int),&s,sizeof(short));
		const char* b = buf;
		LEUD::StreamFix stream(b,200,sizeof(int)+sizeof(short));
		int io=0;
		short so=0;
		stream>>io>>so;
		ASSERT(stream);
		ASSERT(i==io);
		ASSERT(s==so);
		int eo =0;
		stream>>eo;
		ASSERT( !stream );
	}
	void testStreamFlag()
	{
		char buf[200];
		LEUD::StreamFix stream(buf,100);
		unsigned int i = 0xFFFFFFFF;
		unsigned short s = 0xFFFF;
		unsigned int ii = 4;
		unsigned short ss = 5;
		ee e;e.i = 1;e.s = 2;e.ui = 4;e.us=3;
		stream<<LEUD::Stream::NetLocalTran<<i<<s<<ii<<ss<<LEUD::Stream::SupportStruct<<e;
		ASSERT(stream);
		unsigned int io=0;
		unsigned short so=0;
		unsigned int iio = 4;
		unsigned short sso = 5;
		ee eo;memset(&eo,0,sizeof(ee) );
		stream<<LEUD::Stream::ResetFlag;
		stream>>LEUD::Stream::NetLocalTran>>io>>so>>LEUD::Stream::NoNetLocalTran>>iio>>sso>>LEUD::Stream::SupportStruct>>eo;
		ASSERT(stream);
		ASSERT(i==io);
		ASSERT(s==so);
		ASSERT(ii!=iio);
		ASSERT(ss!=sso);
		ASSERT(ii==ntohl(iio));
		ASSERT(ss==ntohs(sso));
		ASSERT(e.i == eo.i);
		stream<<LEUD::Stream::ResetFlag;
		stream<<eo;
		ASSERT(!stream);
	}
	void testStreamString()
	{
		char buf[20000];
		LEUD::StreamFix stream(buf,20000);
		std::string str="erer";
		std::string strs = "tretre";
		std::string stri = "qweqwe";
		std::string strbig(1500,'a');
		stream<<str<<strbig<<LEUD::Stream::StringHeader<<strs<<strbig<<LEUD::Stream::StringIntHeader<<stri<<strbig;
		ASSERT(stream);
		stream<<LEUD::Stream::ResetFlag;
		std::string strout;
		std::string strsout;
		std::string striout;
		std::string strbig1;
		std::string strbig2;
		std::string strbig3;
		stream>>strout>>strbig1>>LEUD::Stream::StringHeader>>strsout>>strbig2>>LEUD::Stream::StringIntHeader>>striout>>strbig3;
		ASSERT(stream);
		ASSERT(str==strout);
		ASSERT(strs==strsout);
		ASSERT(stri==striout);
		ASSERT(strbig==strbig1);
		ASSERT(strbig==strbig2);
		ASSERT(strbig==strbig3);
	}
};

REGISTER_FIXTURE( TestStreamFix );
